package logic

import (
	"context"
	"gorm.io/gorm"
	"three-sxp/models/mysql"

	"three-sxp/userrpc/internal/svc"
	"three-sxp/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowAuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowAuthLogic {
	return &ShowAuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowAuthLogic) ShowAuth(in *userrpc.ShowAuthRequest) (*userrpc.ShowAuthResponse, error) {
	// todo: add your logic here and delete this line
	user, _ := mysql.GetUserInfo(mysql.User{
		Model: gorm.Model{ID: uint(in.UserId)},
	})
	if user.ID == 0 {
		return &userrpc.ShowAuthResponse{
			Code: 400,
			Msg:  "账号不存在",
		}, nil
	}
	if user.Name != "" && user.IdCard != "" {
		return &userrpc.ShowAuthResponse{
			Code: 400,
			Msg:  "账号已经实名",
		}, nil
	}
	u, _ := mysql.GetUserInfo(mysql.User{
		Name:   in.Name,
		IdCard: in.IdCard,
	})
	if u.ID != 0 {
		return &userrpc.ShowAuthResponse{
			Code: 400,
			Msg:  "实名信息已被使用",
		}, nil
	}
	return &userrpc.ShowAuthResponse{
		Code: 200,
		Msg:  "账号未实名",
	}, nil
}
